import { getToken, removeToken, setToken } from "@/utils/auth"
import { Message } from "element-ui"
import axios from "axios"
import router from "@/router"

const state = {
  token: getToken(),
  userInfo: '',
  roles: [],
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_USER_INFO: (state, userInfo) => {
    sessionStorage.setItem('userInfo', userInfo)
    state.userInfo = userInfo
  },
  SET_USER_SIG: (state, userSig) => {
    state.userSig = userSig
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
    sessionStorage.setItem('roles', roles)
  },
}

const actions = {
  login({ commit, dispatch }, userInfo) {
    return new Promise((resolve, reject) => {
      axios
         .post(process.env.VUE_APP_BASE_API + "/users/login", userInfo)
         .then((res) => {
           if (res.data.success) {
             const userInfo = res.data.data.user
             console.log('this1---userInfo-', userInfo)
             commit("SET_TOKEN", res.data.data.token)
             commit("SET_USER_INFO", JSON.stringify(userInfo))
             setToken(res.data.data.token)
             resolve(res.data.data)
           } else {
             Message.error(res.data.message)
             setTimeout(() => {
               setToken('')
               router.push('/login')
             }, 500);
             reject("出错")
           }
         })
    })
  },
  // get user info
  getUserInfo({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      axios.defaults.headers.common["authorization"] = getToken()
      axios
         .post(process.env.VUE_APP_BASE_API + "/users/getUserInfo", userInfo)
         .then(({ data }) => {
           console.log('data', data)
           if (data.success) {
             commit("SET_ROLES", [data.data.role])
             commit("SET_USER_INFO", JSON.stringify(data.data))
             resolve(data.data)
           } else {
             Message.error(data.message)
             setTimeout(() => {
               setToken('')
               router.push('/login')
             }, 500);
             reject("出错")
           }
         })
         // axios
         //    .get(process.env.VUE_APP_BASE_API + "/v1/security/getCurrentUserInfo")
         //    .then((res) => {
         //      if (res.data.success) {
         //        if (!res.data.data) {
         //          Message.error("请重新登录！")
         //          commit("SET_TOKEN", "")
         //          removeToken()
         //          router.push({ path: "/home" })
         //        } else {
         //          const result = res.data.data.user
         //          const group = res.data.data.group
         //          const user = {
         //            id: result.userId,
         //            userId: result.userNo,
         //            userName: result.userName,
         //            userType: result.type,
         //            districtId: group.districtId
         //          }
         //          commit("SET_TOKEN", res.data.data.accessToken)
         //          commit("SET_USER_INFO", user)
         //          resolve(res.data.data)
         //        }
         //      } else {
         //        Message.error(res.data.message)
         //        setTimeout(() => {
         //          setToken('')
         //          router.push('/login')
         //        }, 500);
         //      }
         //    })
         .catch((error) => {
           reject(error)
         })
    })
  },
  // user logout
  logout({ commit }) {
    return new Promise((resolve, reject) => {
      // axios
      //   .get(process.env.VUE_APP_BASE_API + "/v1/security/logout")
      //   .then((res) => {
      //     if (res.data.success) {
      commit("SET_USER_INFO", "")
      commit("SET_TOKEN", "")
      commit("SET_ROLES", [])
      removeToken()
      resolve()
      // }
      // })
      // .catch((error) => {
      //   reject(error)
      // })
    })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
